<template>
  <div>
    <div class="options">
      <el-button type="primary" round plain @click="info.isShow = true"
        >新增</el-button
      >
    </div>
    <el-table
      :data="menuList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="序号" min-width="180">
      </el-table-column>
      <el-table-column prop="title" label="菜单标题" min-width="180">
      </el-table-column>
      <el-table-column prop="icon" label="菜单图标" min-width="180">
        <!-- 通过v-slot接收作用域插槽分发的数据; props是自定义模板变量
            el-table-column组件内部封装了一个作用域插槽
         -->
        <template v-slot="props">
          <!-- <el-button @click="handle(props)">测试</el-button> -->
          <i :class="props.row.icon" style="font-size: 20px"></i>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="菜单地址" min-width="180">
      </el-table-column>
      <el-table-column prop="status" label="菜单状态" min-width="180">
        <template v-slot="props">
          <el-button
            type="primary"
            plain
            size="mini"
            round
            v-if="props.row.status === 1"
            >正常</el-button
          >
          <el-button type="success" plain size="mini" round v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template slot-scope="$props">
          <el-button
            type="primary"
            round
            plain
            size="mini"
            @click="edit($props.row)"
            >编辑</el-button
          >
          <el-button
            @click="del($props.row.id)"
            type="warning"
            round
            plain
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 调用对话框组件 -->
    <Dialog ref="dialog" :info="info" />
  </div>
</template>

<script>
// 导入对话框组件
import Dialog from "./Dialog";
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  methods: {
    ...mapActions("menu", ["getMenuListAction"]),
    // handle(props){
    //   console.log(props);
    // },
    // 编辑按钮点击事件处理函数
    edit(row) {
      // 1-显示对画框组件
      this.info.isShow = true;
      // 2-设置表单元素的值
      this.$refs.dialog.setFormData(row);
    },
    // 菜单删除
    del(id) {
      this.$myConfirm(() => {
        this.$axios.post("/menudelete", { id }).then((res) => {
          if (res.code === 200) {
            this.$message.success(res.msg);
            // 更新数据列表
            this.getMenuListAction();
          } else {
            this.$message.error(res.msg);
          }
        });
      });
    },
  },
  components: { Dialog },
  computed: {
    // ...mapState({menuList:state=>state.menu.menuList})
    ...mapState("menu", ["menuList"]),
  },
  created() {
    // 请求菜单列表
    this.getMenuListAction();
  },
  data() {
    return {
      info: {
        // 控制对话框组件的显示状态, false:表示不显示
        isShow: false,
      },
    };
  },
};
</script>

<style scoped>

</style>